import {createRouter, createWebHistory} from "vue-router"
//@ts-ignore
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import useHomeStore from "@/store/home";
import useDialogStore from "@/store/dialog";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: "/home",
            component: () => import("@/pages/home/index.vue"),
            meta: {
                title: '首页'
            }
        },
        {
            path: "/hospital",
            component: () => import("@/pages/hospital/index.vue"),
            children: [
                {
                    path: "/hospital/register",
                    component: () => import("@/pages/hospital/register/index.vue"),
                    meta: {
                        title: '预约挂号'
                    }
                },
                {
                    path: "/hospital/detail",
                    component: () => import("@/pages/hospital/detail/index.vue"),
                    meta: {
                        title: '医院详情'
                    }
                },
                {
                    path: "notice",
                    component: () => import("@/pages/hospital/notice/index.vue"),
                    meta: {
                        title: '预约通知'
                    }
                },
                {
                    path: "suspend",
                    component: () => import("@/pages/hospital/suspend/index.vue"),
                    meta: {
                        title: '停诊信息'
                    }
                },
                {
                    path: "search",
                    component: () => import("@/pages/hospital/search/index.vue"),

                    meta: {
                        title: '查询'
                    }
                },
                {
                    path: "register_setup",
                    component: () => import("@/pages/hospital/register/register_setup/index.vue")
                    ,
                    meta: {
                        title: '预约第一步'
                    }
                },
                {
                    path: "register_setup_2",
                    component: () => import("@/pages/hospital/register/register_setup_2/index.vue")
                    , meta: {
                        title: '预约第二步'
                    }
                }
            ]
        },
        {
            path: "/user",
            component: () => import("@/pages/user/index.vue"),
            children: [
                {
                    path: "certification",
                    component: () => import("@/pages/user/certification/index.vue")

                    , meta: {
                        title: '实名认证'
                    }
                },
                {
                    path: "feedback",
                    component: () => import("@/pages/user/feedback/index.vue")


                },
                {
                    path: "order",
                    component: () => import("@/pages/user/order/index.vue")

                    , meta: {
                        title: '挂号订单'
                    }
                },
                {
                    path: "patient",
                    component: () => import("@/pages/user/patient/index.vue")

                    , meta: {
                        title: '就诊人管理'
                    }
                },
                {
                    path: "profile",
                    component: () => import("@/pages/user/profile/index.vue")
                },
            ]
        },
        {
            path: "/",
            redirect: "/home"
        }
    ],
    // 切换页面滚动到最顶部滚动条
    scrollBehavior() {
        return {
            left: 0,
            top: 0
        }
    }
});
//路由鉴权
//进度条的加载小圆球不要
NProgress.configure({showSpinner: false});

//定义不需要登录的路由
const whiteList = ["/home", '/hospital/register', '/hospital/detail', '/hospital/notice', '/hospital/close', '/hospital/search'];

router.beforeEach((to: any, from: any, next: any) => {
    NProgress.start()
    const dialogStore = useDialogStore();
    //动态设置网页左上角的标题
    document.title = `尚医通-${to.meta.title}`;
    if (whiteList.includes(to.path)) {
        //放行
        next()
    } else {
        //需要登录，判断是否登录
        if (dialogStore.userInfo.token) {
            //方向
            next()
        } else {
            //弹出弹窗登录窗口
            dialogStore.isShow = true
            //重定向到首页
            next({path: '/home', query: {redirect: to.fullPath}})
        }
    }
})

router.afterEach((to: any, from: any, next: any) => {
    NProgress.done()
})

export default router
